package com.bawei.module_launcher.splash

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.bawei.library_base.BaseApplication
import com.bawei.library_mvi.ui.BaseMviActivity
import com.bawei.module_launcher.FrameActivity
import com.bawei.module_launcher.R
import com.bawei.module_launcher.databinding.LauncherActivitySplashBinding
import com.bawei.module_launcher.user.LoginActivity
import io.virtualapp.hook_sdk.firewall.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class SplashActivity : BaseMviActivity<LauncherActivitySplashBinding>() {
    private lateinit var viewModel:SplashViewModel
    private var handler = Handler()
    lateinit var hookSDK : HookSDK
    override fun handleState() {
        lifecycleScope.launch{
            viewModel.states.collect{
                when(it){
                    is SplashState.SplashLockState ->{//锁屏
                        showMsg("屏幕锁定不能使用，请联系管理员解锁")
                        finish()
                    }
                    is SplashState.SplashUnLockState ->{}
                    is SplashState.Init ->{}
                    is SplashState.SplashWhiteState->{//网络请求白名单：只允许访问的接口
                        //白名单
                        var array = ArrayList<AppNetInfo>()
                        it.list.forEach {
                            val info = AppNetInfo("",it.url,"")
                            array.add(info)
                        }
                        //防火墙技术----------
                        FireWallConfig.array = array //黑名单网址列表
                        FireWallConfig.type = FireWallType.BLACK //当作黑名单处理
                        val build = HookSDK.Build()
                        build.configHookType(HookType.All)//hook 全部 webview okhttp
//                        FireWallConfig.whiteStr = AppConfig().parseAssets("white.xml")
                        build.init(BaseApplication.instance,"com.bawei.a2010mdm",null)
                        hookSDK = build.build()
//                        hookSDK.hook()
                        //防火墙技术----------
                        handler.postDelayed({
                            finish()
                            val intent = Intent(this@SplashActivity, FrameActivity::class.java)
                            startActivity(intent)
                        },3000)






                    }

                }
            }
        }

    }

    override fun getLayout(): Int {
        return R.layout.launcher_activity_splash

    }

    override fun initData() {
        super.initData()



        lifecycleScope.launch {
            viewModel.intents.send(SplashIntent.SplashWhiteIntent)
        }
    }

    override fun generateViewModel() {
        viewModel = ViewModelProvider(this).get(SplashViewModel::class.java)
    }

    //避免内存泄漏
    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)
    }

}